<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { Plus } from '@element-plus/icons-vue';
import type { userInfoRes} from '@/types/user';
import {  editUserAPI } from '@/apis/card';
import { getUserAPI } from '@/apis/user';

const user = ref<Partial<userInfoRes>>({
  id: '1',
  name: 'exampleUser',
  photo: 'https://example.com/photo.jpg',
  real_name:'',
  gender: 0,
  birthday: '2018-12-20',
  intro: '这是一个示例个人介绍。',
});

const editUser = async()=>{
  const res = await editUserAPI(user.value)
  console.log('resUser',res);
  getUser()
}

const getUser = async()=>{
  const res = await getUserAPI()
  user.value = res.data.data
  // getUser()
}
onMounted(()=>{

  getUser()
})
</script>

<template>
  <div class="user-profile">
    <el-card shadow="always">

      <template #header>
        <el-breadcrumb>
          <el-breadcrumb-item to="/">首页</el-breadcrumb-item>
          <el-breadcrumb-item>个人信息</el-breadcrumb-item>
        </el-breadcrumb>
      </template>

      <el-form :model="user" label-width="100px">
        <el-form-item label="头像">
          <el-upload
            class="avatar-uploader"
            action=""
            list-type="picture-card"
          >
            <el-icon><Plus /></el-icon>
          </el-upload>
        </el-form-item>
        <el-form-item label="真实用户名">
          <el-input v-model="user.real_name" placeholder="真实用户名"></el-input>
        </el-form-item>
        <el-form-item label="ID">
          <el-input v-model="user.id" placeholder="ID" disabled></el-input>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="user.name" placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-select v-model="user.gender" placeholder="请选择性别">
            <el-option label="男" :value="0"></el-option>
            <el-option label="女" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生日">
          <el-date-picker
            v-model="user.birthday"
            type="date"
            placeholder="选择日期"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="个人介绍">
          <el-input
            v-model="user.intro"
            type="textarea"
            :rows="4"
            placeholder="请输入个人介绍"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <div class="user-actions">
            <el-button type="primary" @click="editUser">保存</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<style scoped>
.user-profile {
  /* display: flex; */
  /* justify-content: center; */
  margin-top: 20px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}

.avatar {
  width: 100px;
  height: 100px;
  display: block;
}

.user-actions {
  margin-top: 20px;
  text-align: right;
}

</style>
